﻿# -*- coding:utf-8 -*-
# 动态渲染selenium爬取百度图片街拍
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
import time, os
from pyquery import PyQuery
from urllib.request import urlretrieve

# 创建一个浏览器对象
driver = webdriver.Chrome(r"D:/tools/softwave/python/Scripts/chromedriver.exe")


def browser():
    """模拟浏览器来进行操作"""

    try:
        # 打开百度图片首页
        driver.get("http://image.baidu.com")
        # 显式等待10秒 等到搜索框出来就行id="kw"
        wait = WebDriverWait(driver, 10)
        wait.until(EC.presence_of_element_located((By.ID, 'kw')))
        # 输入“街拍”关键字然后回车进行搜索
        input = driver.find_element_by_id("kw")
        input.send_keys("街拍")
        input.send_keys(Keys.ENTER)
        time.sleep(4)
        # 模拟下拉页面滚动条，下拉n次，获得n+1个imagepage
        for i in range(9):
            print(f"下拉滚动条{i+1}次!")
            driver.execute_script('window.scrollTo(0, document.body.scrollHeight)')
            time.sleep(3)

    except TimeoutException:
        browser()


def get_image():
    """获取图片"""
    # 获取网页响应的HTML，
    html = driver.page_source
    doc = PyQuery(html)
    # print(html)
    items = doc("#imgid .imgpage .main_img").items()
    return items


def save_image(items):
    """保存图片"""
    # 设置保存路径
    if not os.path.exists("./baidu"):
        os.mkdir("./baidu")
    if not os.path.exists("./baidu/photos"):
        os.mkdir("./baidu/photos")
    if items:
        print("图片爬取成功！保存中...")
        for item in items:
            url = item.attr('src')
            path = "./baidu/photos/"+url.split('/').pop()
            # 如果有重名的文件，就删掉它
            if os.path.exists(path):
                os.remove(path)
            try:
                urlretrieve(url, path)
            except Exception as err:
                if hasattr(err, 'code'):
                    print("HTTPError!")
                    print(err.code)
                elif hasattr(err, 'reason'):
                    print("URLError!")
                    print(err.reason)

        return True
    else:
        return False


def main():
    """主函数"""
    # 模拟浏览器操作
    browser()
    # 调用get_image()来获取图片
    items = get_image()
    # 保存图片
    if not save_image(items):
        print("保存失败！")
    else:
        print("保存成功！")
    # 关闭浏览器对象
    driver.close()


if __name__ == '__main__':
    main()
